using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Collections.ObjectModel;

namespace Microsoft.PowerShell.CmdletManagement
{
    public partial class PsSpecCmdletParameterControl : System.Windows.Controls.UserControl
    {
        public PsSpecCmdletParameterControl()
        {
            InitializeComponent();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            CmdletDesignerUtils.DisableAllCustomControls(this);

            if (CmdletDesignerUtils.IsInRole(PsSpecProjectRole.SpecWriter, Application.Current))
            {
                ParameterName.IsReadOnly = false;
                ParameterType.IsReadOnly = false;
                Description.IsReadOnly = false;
                Notes.IsReadOnly = false;
                Position.IsReadOnly = false;
                DefaultValue.IsReadOnly = false;
                Aliases.IsReadOnly = false;
                IsDynamic.IsReadOnly = false;
                Mandatory.IsReadOnly = false;
                ValueFromPipeline.IsReadOnly = false;
                ValueFromPipelineByPropertyName.IsReadOnly = false;
                ValueFromRemainingArguments.IsReadOnly = false;
                Parameters.IsReadOnly = false;
                AllowGlobbing.IsReadOnly = false;
                AllowEmptyCollection.IsReadOnly = false;
                AllowEmptyString.IsReadOnly = false;
                AllowNull.IsReadOnly = false;
                ValidateCount.IsReadOnly = false;
                ValidateNotNull.IsReadOnly = false;
                ValidateNotNullOrEmpty.IsReadOnly = false;
                ValidateRange.IsReadOnly = false;
                ValidateSet.IsReadOnly = false;
                ValidateLength.IsReadOnly = false;
                ValidatePattern.IsReadOnly = false;
                Custom1.IsReadOnly = false;
                Custom2.IsReadOnly = false;
                Custom3.IsReadOnly = false;
                Custom4.IsReadOnly = false;
            }

            if (CmdletDesignerUtils.IsInRole(PsSpecProjectRole.HelpWriter, Application.Current))
            {
                HelpDescription.IsReadOnly = false;
                DefaultValue.IsReadOnly = false;
            }

            base.OnRender(drawingContext);
        }

        // The parameter control lost focus
        private void LostFocus_Parameters(object sender, System.Windows.RoutedEventArgs e)
        {
            ApplyChanges();
        }

        public void ApplyChanges()
        {
            // Put the item from the "New Set" dialog into the selected
            // parameters if it is valid.
            string newSetName = Parameters.NewSetName.Text;
            if ((newSetName != null) && (newSetName.Trim().Length > 0))
            {
                Parameters.AddCurentSetNameToList(newSetName, true);
            }

            List<string> selectedParameterSets = new List<string>();

            ObservableCollection<ParameterSetSelector> allParameterSets =
                (ObservableCollection<ParameterSetSelector>)Parameters.ParameterSetsList.ItemsSource;

            // See which ones are checked
            if (((bool)Parameters.SpecificSetRadioButton.IsChecked) &&
                (Parameters.ParameterSetsList.HasItems))
            {
                foreach (ParameterSetSelector item in allParameterSets)
                {
                    if (item.IsChecked)
                    {
                        selectedParameterSets.Add(item.ParameterSetName);
                    }
                }
            }
            else
            {
                selectedParameterSets.Add(null);
            }

            PsSpecCmdletParameterDetails currentDetails = (PsSpecCmdletParameterDetails)this.DataContext;
            currentDetails.ParameterSets = selectedParameterSets;
        }
    }
}